package com.bank.dao;

import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.bank.entity.Teller;
import com.bank.excption.CommonException;

@Repository(value="tellerDao")
public class TellerDaoImpl implements TellerDao {

	@Autowired
	SessionFactory sessionFactory;
	
	@SuppressWarnings("unchecked") 
	public List<Object> getTellerList(Map<String, Object> params) {
		String sql = "from Teller where 1=1 ";
		Integer offsetInt  = (Integer)params.get("offset");
		Integer limitInt = (Integer)params.get("limit");
		String teller_number = (String)params.get("teller_number");
		String teller_name  = (String)params.get("teller_name");
		String teller_status = (String)params.get("teller_status");
		if(teller_number!=null && teller_number.equals("")==false){
			sql = sql+" and teller_number ='"+teller_number+"'";
		}
		if(teller_name != null && teller_name.equals("")==false){
			sql = sql+" and  teller_name='"+teller_name+"'";
		}
		if(teller_status != null && teller_status.equals("")==false){
			sql = sql+"  and teller_status='"+teller_status+"'";
		}
		
		Session  session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(sql);
		query.setFirstResult(offsetInt);
		query.setMaxResults(limitInt);
		List<Object> accountList = query.list();	
		return accountList;		
	}

	public Integer getTellerListCount(Map<String, Object> params) {
		String sql = "select count(*)  from Teller where 1=1 ";
		String teller_number = (String)params.get("teller_number");
		String teller_name  = (String)params.get("teller_name");
		String teller_status = (String)params.get("teller_status");
		if(teller_number!=null && teller_number.equals("")==false){
			sql = sql+" and teller_number ='"+teller_number+"'";
		}
		if(teller_name != null && teller_name.equals("")==false){
			sql = sql+" and  teller_name='"+teller_name+"'";
		}
		if(teller_status != null && teller_status.equals("")==false){
			sql = sql+"  and teller_status='"+teller_status+"'";
		}
		
		Session  session = sessionFactory.getCurrentSession();
		int totalRecord = Integer.parseInt(session.createQuery(sql).uniqueResult().toString());
		return totalRecord;
	}

	/**
	 * 根据柜员编号获取柜员信息
	 * @param tellerNumber   获取柜员信息
	 * @return 柜员信息
	 * */
	@SuppressWarnings("unchecked") 
	public Teller getTellerByNumber(String tellerNumber) {
		// TODO Auto-generated method stub
		String sql = "from Teller where teller_number='"+tellerNumber+"'";
		Session session = sessionFactory.getCurrentSession();
		Query query=session.createQuery(sql);
		List<Teller> list = query.list();
		Teller teller = null;
		if(list.size()==0)
			return teller;
		for(Teller tellerTmp:list){
			teller = tellerTmp;
		}
		return teller;
	}
	/**
	 * 
	 * 更新柜员信息
	 * @param 
	 * @throws CommonException 
	 * 
	 * */
	public void updateTellerInfo(Teller teller) {
		Session session = sessionFactory.getCurrentSession();
		session.update(teller);
		session.flush();
	}
	
	/**
	 * 获取柜员流水序号
	 * @param telerId 柜员代号
	 * @return  序号
	 * */
	public String getTellerSerno(String tellerId){
		Teller  teller = this.getTellerByNumber(tellerId);
		String sernoString  =  teller.getSerial_number();
		Integer sernoInt = Integer.parseInt(sernoString);
		String serNoString = sernoInt.toString();
		if(sernoInt == 9999){
			sernoInt = 1000;
		}
		sernoInt = sernoInt+1;
		sernoString = sernoInt.toString();
		teller.setSerial_number(sernoString);
		this.updateTellerInfo(teller);
		return serNoString;
	}

	public void save(Teller teller) {
		Session session = sessionFactory.getCurrentSession();
		session.save(teller);
		session.flush();
	}
	
	
	

}
